package com.pitb.citizenreporting.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBTableHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "WatSanHack.db";
	private static final int DATABASE_VERSION = 1;

	public DBTableHelper(Context paramContext) {
		super(paramContext, DATABASE_NAME, null, DATABASE_VERSION);
	}

	private void createDownloadTable(SQLiteDatabase sqlitDatabase) {
		sqlitDatabase.execSQL("CREATE TABLE IF NOT EXISTS  "
				+ DBInterfaces.Tables.PROBLEM + " ( "
				+ DBInterfaces.PROBLEM.ID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ DBInterfaces.PROBLEM.PROBLEM + " TEXT,"
				+ DBInterfaces.PROBLEM.STATUS + " TEXT,"
				+ DBInterfaces.PROBLEM.STREET + " TEXT," 
				+ DBInterfaces.PROBLEM.AREA + " TEXT,"
				+ DBInterfaces.PROBLEM.CITY + " TEXT,"
				+ DBInterfaces.PROBLEM.PROVINCE + " TEXT,"
				+ DBInterfaces.PROBLEM.CONTACT + " TEXT,"
				+ DBInterfaces.PROBLEM.DATE + " TEXT,"
				+ DBInterfaces.PROBLEM.TRACKING_ID + " TEXT,"
				+ DBInterfaces.PROBLEM.REVISIONS + " INTEGER,"
				+ DBInterfaces.PROBLEM.TYPE + " TEXT,"
				+ DBInterfaces.PROBLEM.COMMENTS + " TEXT"
				+ ")");
		Log.i("Reporting App",DBInterfaces.Tables.PROBLEM + " Table created");
	}	

	public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
		createDownloadTable(paramSQLiteDatabase);		
	}

	public void onUpgrade(SQLiteDatabase paramSQLiteDatabase, int paramInt1,
			int paramInt2) {
		Log.i("Reproting app","onUpgrade() from " + paramInt1 + " to " + paramInt2);
		if (paramInt2 != 1) {
			Log.i("Reproting app","Destroying old data during upgrade");
			paramSQLiteDatabase
					.execSQL("DROP TABLE IF EXISTS " + DBInterfaces.Tables.PROBLEM);			
			onCreate(paramSQLiteDatabase);
		}
	}
}
